//
//  LLShopViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/10.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLShopViewController.h"
#import "LLShopGoodsCCell.h"
#import "LLShopGoodsModel.h"
#import "LLGoodsDetailViewController.h"

@interface LLShopViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, LLShopGoodsCCellDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@end

@implementation LLShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationBar];
    [self initUI];
    [self initData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = UIColor.blackColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:LLRegularFont(18)}];
    self.navigationController.navigationBar.barTintColor = UIColor.whiteColor;
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.tintColor = UIColor.whiteColor;
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:LLRegularFont(18)}];
//    self.navigationController.navigationBar.barTintColor = LLColor(218, 13, 13);
////    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"底"] forBarMetrics:UIBarMetricsDefault];
//}

- (void)initData {
//    for (NSInteger i = 0; i < 11; i++) {
//        LLShopGoodsModel *model = LLShopGoodsModel.new;
//        model.title = @"满200-100元优惠券满满200-100元优惠券满满200-100元优惠券满满200-100元优惠券满";
//        model.imgUrl = @"";
//        model.orginalPrice = @"3.4";
//        model.countStr = @"13";
//        [self.models addObject:model];
//    }
//    [self.collectionView reloadData];
}

- (void)initNavigationBar {
    LLButton *followBtn = [LLInitBaseViewTool buttonWithTitle:@"关注" image:nil titleColor:UIColor.whiteColor font:15];
    followBtn.size = CGSizeMake(60, 30);
    followBtn.backgroundColor = LLColor(225, 0, 0);
    followBtn.layer.cornerRadius = 5;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:followBtn];
}

- (void)initUI {
    UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    CGFloat itemW = (LLScreenWidth-10*3)/2.0;
    CGFloat itemH = itemW*230/174 + 60;
    layout.itemSize = CGSizeMake(itemW, itemH);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight-self.navH) collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = LLColorRGB(244);
    [_collectionView registerClass:[LLShopGoodsCCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0);
    [self.view addSubview:_collectionView];
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.models.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LLShopGoodsCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.model = self.models[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LLGoodsDetailViewController *vc = LLGoodsDetailViewController.new;
    vc.model = self.models[indexPath.item];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - LLShopGoodsCCellDelegate

- (void)ccellDidClickShoppingCart:(LLShopGoodsCCell *)ccell {
    LLShopGoodsModel *model = ccell.model;
}

@end
